
#include "area.h"
#include "mob.h"
#include "mysql.h"
#include "world.h"

Mob::Mob( void ) {
  type( TYPE_MOB, true );
  return;
}

Mob::Mob( const Mob& ref ): Creature( ref ) {
  vnum( ref.vnum() );
  return;
}

Mob::Mob( ROW row ) {
  ID( row["mobID"] );
  vnum( row["vnum"] );
  name( row["name"] );
  unserializeKeywords( row["keywords"] );
  description( row["description"] );
  level( row["level"] );
  return;
}

Mob::Mob( Area* area, const unsigned long& vnum ): Creature() {

  try {
    char query[MAX_BUFFER];

    sprintf( query, "INSERT IGNORE INTO mobs ( areaID, vnum ) VALUES ( %lu, %lu );", area->ID(), vnum );
    World::Instance().getMysql()->insert( query );
    ID( World::Instance().getMysql()->getInsertID() );

    area->mobs().insert( std::make_pair( vnum, this ) );
    this->vnum( vnum );

  } catch ( MysqlException me ) {
    fprintf( stderr, "Failed to create mob for area %lu: %s\n", area->ID(), me.getMessage().c_str() );
    return;
  }

  return;
}

Mob::~Mob( void ) {
  return;
}

/******************************************************* Overloads of pure virtual methods ********************************************************/
bool Mob::save( void ) {
  try {
    Mysql* mysql = World::Instance().getMysql();
    char query[MAX_BUFFER];

    sprintf( query,
      "UPDATE mobs SET        \
        name = '%s',          \
        keywords = '%s',      \
        description = '%s',   \
        level = %d            \
       WHERE mobID = %lu      \
       LIMIT 1;",
      Mysql::addslashes(name()).c_str(),
      Mysql::addslashes(serializeKeywords()).c_str(),
      Mysql::addslashes(description()).c_str(),
      level(),
      ID()
    );
    mysql->update( query );

  } catch ( MysqlException me ) {
    fprintf( stderr, "Failed to save mob %lu: %s\n", ID(), me.getMessage().c_str() );
    return false;
  }

  return true;
}

bool Mob::destroy( void ) { // public
  unsigned long tempID = ID();
  try {
    char query[MAX_BUFFER];

    sprintf( query,
      " DELETE                \
        FROM mobs             \
        WHERE mobID = %lu     \
        LIMIT 1;",
      ID()
    );
    World::Instance().getMysql()->remove( query );
    delete this;

  } catch ( MysqlException me ) {
    fprintf( stderr, "Failed to delete mob %lu: %s\n", tempID, me.getMessage().c_str() );
    return false;
  }
  return true;
}

/******************************************************* Overloads of virtual methods ********************************************************/



/******************************************************* Static methods ********************************************************/
Mob* Mob::create( Area* area, const unsigned long& vnum ) { // public static
  return new Mob( area, vnum );
}

Mob* Mob::create( ROW row ) { // public static
  return new Mob( row );
}

Mob* Mob::create( Mob* mob, Room* room ) { // public static
  Mob* m = new Mob( *mob );
  m->room( room );
  return m;
}

std::string Mob::getInformation( Mob* mob ) { // public static
  std::string output;
  char buffer[MAX_BUFFER];

  output.append( "  --== {Ybasic mob data{x ==--\n" );
  // Basic mob information...
  sprintf( buffer, "vnum......... {y%lu{x\n\
name......... {y%s{x\n\
level........ {y%u{x\n\
gender....... {y%s{x\n\
keywords..... {y%s{x\n\
description.. %s\n\
", mob->vnum(), mob->name().c_str(), mob->level(), mob->genderName(), mob->getKeywordList().c_str(), mob->description().c_str() );
  output.append( buffer );

  return output;
}

